package com.example.housekeeping.controller;

import com.example.housekeeping.pojo.dto.AdminAddNewDTO;
import com.example.housekeeping.pojo.dto.WorkerAddNewDTO;
import com.example.housekeeping.pojo.dto.WorkerUpdateDTO;
import com.example.housekeeping.pojo.entity.Worker;
import com.example.housekeeping.pojo.vo.WorkerListItemVO;
import com.example.housekeeping.pojo.vo.WorkerStandardVO;
import com.example.housekeeping.pojo.vo.WorkerVo;
import com.example.housekeeping.service.WorkerService;
import com.example.housekeeping.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * 家政服务员(Worker)表控制层
 *
 * @author Xhe
 * @since 2023-03-30 14:26:08
 */
@Slf4j
@RestController
@RequestMapping("/workers")
@Api(tags = "05.服务员信息模块")
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    public WorkerController(){
        log.debug("创建控制器类对象:WorkerController");
    }

    @ApiOperation("添加服务员")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(WorkerAddNewDTO workerAddNewDTO) {
        log.debug("开始处理【添加服务员】的请求，参数：{}", workerAddNewDTO);
        workerService.addNew(workerAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据ID删除服务员")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "服务员ID", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【根据ID删除服务员】的请求，参数：{}", id);
        workerService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据ID修改服务员")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "服务员ID", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> update(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id, @Valid WorkerUpdateDTO workerUpdateDTO) {
        log.debug("开始处理【根据ID修改服务员】的请求，参数：{},新数据:{}", id,workerUpdateDTO);
        workerService.updateById(id,workerUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("查询服务员列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<WorkerListItemVO>> list() {
        log.debug("开始处理【查询服务员列表】的请求，无参数");
        List<WorkerListItemVO> list = workerService.list();
        return JsonResult.ok(list);
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    @ApiOperationSupport(order = 600)
    public JsonResult<WorkerVo> login(WorkerAddNewDTO workerAddNewDTO, HttpSession session){
        log.debug("开始处理服务员登录业务，当前参数：{}",workerAddNewDTO);
        WorkerVo login = workerService.login(workerAddNewDTO, session);
        return JsonResult.ok(login);
    }

    @PostMapping("/register")
    @ApiOperation("注册")
    @ApiOperationSupport(order = 700)
    public JsonResult<Void> register(WorkerAddNewDTO workerAddNewDTO){
        log.debug("开始处理服务员注册业务，当前参数：{}",workerAddNewDTO);
        workerService.register(workerAddNewDTO);
        return JsonResult.ok();
    }

    @GetMapping("/selectById")
    @ApiOperation("查询单条数据")
    @ApiOperationSupport(order = 810)
    public JsonResult<WorkerStandardVO> selectById(Long id){
        log.debug("开始处理业务,数据Id:{}",id);
        WorkerStandardVO standardById = workerService.getStandardById(id);
        return JsonResult.ok(standardById);
    }
}

